import java.io.*;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;

import org.apache.poi.hwpf.extractor.WordExtractor;


public class ParseMail {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			//get a emai from eml.file
			InputStream in = new FileInputStream("D://multipartemail.eml");
			
			//build a MimeMessage
			Properties props = new Properties();
			// fill props with any information
			Session session = Session.getInstance(props, null);
			MimeMessage msg = new MimeMessage(session,in);
			
			//get Multipart from the email
			Multipart mp = (Multipart) msg.getContent();
		
			//get parts form multipart
			for (int i = 0; i < mp.getCount(); i++) {
				Part part = mp.getBodyPart(i);
				MimeBodyPart mbp = (MimeBodyPart)part;
				//get bodypart which attatchment is
				if(mbp.isMimeType("application/msword")){
					//get file name of the attachment
					String filename = mbp.getFileName();
					System.out.println("attachment's name: "+filename);
					
					//create instance wordExtractor form multipart's inputstream
					WordExtractor extractor = new WordExtractor(mbp.getInputStream());
					
					//get content form the word attachment
					String attachmentContent = extractor.getText();
					System.out.println("content in attachemnt: "+attachmentContent);
				}
//				if(part.getContentType().startsWith("application/msword"));
//				System.out.println("a word file");
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
